$("input[name='identity']").change(function () {
    if (this.value == 'worker') {
        $('#working').removeClass('d-none');
        $('#same').prop('disabled', false);
    } else {
        $('#working').addClass('d-none');
        $('#same').prop('checked', false);
        $('#interested').removeClass('d-none');
        $('#same').prop('disabled', true);
    }
});
$('#same').change(function () {
    if ($(this).prop('checked')) $('#interested').addClass('d-none');
    else $('#interested').removeClass('d-none');
});
let AIclass;
$.ajax({
    type:'get',
    url:'/static/js/AIclassification.json',
    dataType:'json',
    success:function(data){
        AIclass = data;
        for (let i = 1; i < 3; i++) {
            for (let j in data)
                $('#field' + i).append('<option>' + data[j].name + '</option>');
            for (let j in data[0].list)
                $('#task' + i).append('<option>' + data[0].list[j].name + '</option>');
            for (let j in data[0].list[0].list)
                $('#subtask' + i).append('<option>' + data[0].list[0].list[j].name + '</option>');
            for (let j in data[0].list[0].list[0].list)
                $('#benchmark' + i).append('<option>' + data[0].list[0].list[0].list[j] + '</option>');
        }
    }
});
let select4 = ['field', 'task', 'subtask', 'benchmark'];
let chosen = [new Set(), new Set()];
for (let i = 1; i < 3; i++) {
    $('#choose' + i).click(function () {
        let s = '';
        for (let j in select4) {
            s += $('#' + select4[j] + i).find('option:selected').text();
            if (j != 3) s += '>';
        }
        if (! chosen[i - 1].has(s)) {
            $('#chosen' + i).append('<option>' + s + '</option>');
            chosen[i - 1].add(s);
        }
    });
    $('#remove' + i).click(function () {
        $('#chosen' + i).find('option:selected').each(function () {
            chosen[i - 1].delete($(this).text());
            $(this).remove();
        });
    });
    $('#field' + i).change(function () {
        let ind0 = $('#field' + i)[0].selectedIndex;
        $('#task' + i).empty();
        $('#subtask' + i).empty();
        $('#benchmark' + i).empty();
        for (let j in AIclass[ind0].list)
            $('#task' + i).append('<option>' + AIclass[ind0].list[j].name + '</option>');
        for (let j in AIclass[ind0].list[0].list)
            $('#subtask' + i).append('<option>' + AIclass[ind0].list[0].list[j].name + '</option>');
        for (let j in AIclass[ind0].list[0].list[0].list)
            $('#benchmark' + i).append('<option>' + AIclass[ind0].list[0].list[0].list[j] + '</option>');
    });
    $('#task' + i).change(function () {
        let ind0 = $('#field' + i)[0].selectedIndex;
        let ind1 = $('#task' + i)[0].selectedIndex;
        $('#subtask' + i).empty();
        $('#benchmark' + i).empty();
        for (let j in AIclass[ind0].list[ind1].list)
            $('#subtask' + i).append('<option>' + AIclass[ind0].list[ind1].list[j].name + '</option>');
        for (let j in AIclass[ind0].list[ind1].list[0].list)
            $('#benchmark' + i).append('<option>' + AIclass[ind0].list[ind1].list[0].list[j] + '</option>');
    });
    $('#subtask' + i).change(function () {
        let ind0 = $('#field' + i)[0].selectedIndex;
        let ind1 = $('#task' + i)[0].selectedIndex;
        let ind2 = $('#subtask' + i)[0].selectedIndex;
        $('#benchmark' + i).empty();
        for (let j in AIclass[ind0].list[ind1].list[ind2].list)
            $('#benchmark' + i).append('<option>' + AIclass[ind0].list[ind1].list[ind2].list[j] + '</option>');
    });
}
$('#next').click(function () {
    let chosens1 = '';
    if ($("input[name='identity']:checked")[0].value == 'worker') {
        chosen[0].forEach((x) => {
            chosens1 += x + '@';
        });
    }
    let chosens2 = '';
    if ($('#same').prop('checked')) chosens2 = chosens1;
    else {
        chosen[1].forEach((x) => {
            chosens2 += x + '@';
        });
    }
    if (chosens1 == '' && chosens2 == '') {
        alert("At least choose 1 working field or interested field!");
    } else {
        sessionStorage.setItem('working_field', chosens1);
        sessionStorage.setItem('interested_field', chosens2);
        location.href = '/register2.htm';
    }
});
